package main

import (
	"context"
	"golang.org/x/sync/errgroup"
	"log"
	"net/http"
	"os"
	"os/signal"
)

type Server interface {
	start() error
	stop(ctx context.Context) error
}

type Server1 struct {
	server *http.Server
}

func NewServer1() *Server1 {
	s := http.Server{}
	s.Addr = ":9090"
	return &Server1{server: &s}
}

func (s *Server1) start() error {
	return s.server.ListenAndServe()
}
func (s *Server1) stop(ctx context.Context) error {
	return s.server.Shutdown(ctx)
}

func main() {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	eg, ctx := errgroup.WithContext(ctx)
	c := make(chan os.Signal, 1)
	signal.Notify(c)
	servers := make([]Server, 0)

	servers = append(servers, NewServer1())

	eg.Go(func() error {
		select {
		case <-c:
			cancel()
		}
		return nil
	})
	for _, s := range servers {
		s := s
		eg.Go(func() error {
			return s.start()
		})
		eg.Go(func() error {
			<-ctx.Done()
			return s.stop(ctx)
		})
	}
	if err := eg.Wait(); err != nil {
		log.Fatal(err)
	}
}
